package com.choudou5.solr.dataimport.config;

import com.choudou5.solr.util.JsonUtil;
import com.choudou5.solr.util.StrUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * @author choudou5
 * @create 2017-08-下午 6:30
 **/
public class DIXMLTools {

    private static final Logger logger = LoggerFactory.getLogger(DIXMLTools.class);

    public static void main(String[] args) {
        String path = "conf/dataimport.xml";
        List<DIEntity> config = parserXML(path);
        System.out.println(JsonUtil.toStr(config));
    }

    /**
     * 解析xml文件
     * @param path
     * @return
     */
    public static List<DIEntity> parserXML(String path) {
        List<DIEntity> config = new ArrayList<>();
        InputStream is = DIXMLTools.class.getClassLoader().getResourceAsStream(path);
        if(is == null){
            logger.error(path + " 文件不存在！");
            throw new RuntimeException(path + " 文件不存在！");
        }
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = dbFactory.newDocumentBuilder();
            Document doc = builder.parse(is);
            NodeList entitys = doc.getElementsByTagName("entity");
            if (entitys != null && entitys.getLength() > 0) {
                for (int i = 0; i < entitys.getLength(); i++) {
                    DIEntity entity = new DIEntity();
                    Element coll = (Element) entitys.item(i);
                    entity.setName(coll.getAttribute("name"));
                    entity.setDbGroup(coll.getAttribute("dbGroup"));
                    entity.setBatchSize(coll.getAttribute("batchSize"));
                    entity.setIndexName(coll.getAttribute("indexName"));
                    entity.setStartup(coll.getAttribute("startup"));
                    DIQueryPlugin queryPlugin = new DIQueryPlugin();
                    queryPlugin.addFull(getQuery(DIQueryPluginEnum.FULL, coll)).addDelta(getQuery(DIQueryPluginEnum.DELTA, coll));
                    entity.setQueryPlugin(queryPlugin);
                    config.add(entity);
                }
            }
        } catch (ParserConfigurationException | SAXException | IOException e) {
            e.printStackTrace();
            logger.error(path+"， 解析xml失败.", e);
        }
        logger.info("解析完毕");
        return config;
    }

    private static DIQuery getQuery(DIQueryPluginEnum type, Element coll){
        String typeStr = type.name().toLowerCase();
        String queryId = coll.getAttribute(typeStr+"QueryId");
        if(StrUtil.isNotBlank(queryId)){
            DIQuery query = new DIQuery();
            query.setType(type);
            query.setQueryIdSql(queryId);
            query.setQueryCountSql(coll.getAttribute(typeStr+"QueryCount"));
            query.setQuerySql(coll.getAttribute(typeStr+"Query"));
            return query;
        }
        return null;
    }

}
